import 'package:demo202112/utils/common_appbar.dart';
import 'package:flutter/material.dart';
import 'dart:math';

/// @Author wywinstonwy
/// @Date 2022/1/20 8:42 上午
/// @Description:

class MyAnimatedList extends StatefulWidget {
  const MyAnimatedList({Key? key}) : super(key: key);

  @override
  _MyAnimatedListState createState() => _MyAnimatedListState();
}

class _MyAnimatedListState extends State<MyAnimatedList> {
  var data = <ItemModel>[];
  int counter = 5;
  final globalKey = GlobalKey<AnimatedListState>();

  @override
  void initState() {
    for (var i = 0; i < counter; i++) {
      Color color = Color.fromRGBO(Random().nextInt(256), Random().nextInt(256),
          Random().nextInt(256), 1);
      String title = '${i + 1}';
      ItemModel item = ItemModel(backColor: color, title: title);
      data.add(item);
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppBar('AnimatedList'),
      body: Stack(
        children: [_buildAnimatedList(), buildAddBtn()],
      ),
    );
  }

  buildAddBtn() {
    return Positioned(
      child: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          //添加一个列表项目
          // data.add(('${++counter}'));
          Color color = Color.fromRGBO(Random().nextInt(256),
              Random().nextInt(256), Random().nextInt(256), 1);
          String title = '${++counter}';
          ItemModel item = ItemModel(backColor: color, title: title);
          data.add(item);
          // 告诉列表项有新添加的列表项
          globalKey.currentState!.insertItem(data.length - 1);
          print('添加 $counter');
        },
      ),
      bottom: 30,
      left: 0,
      right: 0,
    );
  }

  _buildAnimatedList() {
    return AnimatedList(
        key: globalKey,
        initialItemCount: data.length,
        itemBuilder: (context, index, Animation<double> animation) {
          return FadeTransition(
            opacity: animation,
            child: _buildItem(context, index),
          );
        });
  }

  _buildItem(context, index) {
    ItemModel itemModel = data[index];

    return Container(
      padding: const EdgeInsets.fromLTRB(0, 10, 0, 10),
      margin: const EdgeInsets.only(bottom: 10),
      color: itemModel.backColor,
      key: ValueKey(itemModel.title),
      child: Row(
        children: [
          Container(
            margin: const EdgeInsets.only(left: 10),
            child: Text(
              itemModel.title,
              style: const TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
            ),
          ),
          Expanded(child: Container()),
          IconButton(
              onPressed: () {
                onDelete(context, index);
              },
              icon: const Icon(
                Icons.delete,
                color: Colors.red,
                size: 30,
              )),
        ],
      ),
    );
  }

  onDelete(context, index) {
    //
    globalKey.currentState!.removeItem(index, (context, animation) {
      // 删除过程执行的是反向动画，animation.value 会从1变为0
      var item = _buildItem(context, index);
      data.removeAt(index);
      return FadeTransition(
        opacity: CurvedAnimation(
          parent: animation,
          //让透明度变化的更快一些
          curve: const Interval(0.5, 1.0),
        ),
        child: SizeTransition(
          sizeFactor: animation,
          axisAlignment: 0.0,
          child: item,
        ),
      );
    }, duration: const Duration(milliseconds: 200) //动画时间200毫秒
        );
  }
}

class ItemModel extends StatelessWidget {
  const ItemModel({Key? key, required this.backColor, required this.title})
      : super(key: key);
  final Color backColor;
  final String title;

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
